package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.Prop;
import com.rockcent.mall.Routes;
import com.rockcent.mall.helper.UserHelper;
import com.rockcent.mall.lib.domain.UserInfo;
import com.rockcent.mall.lib.dto.MallDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.repository.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by HK on 16/9/9.
 * 认证
 */
@RestController
public class SSOController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(SSOController.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SysPermissionRepository sysPermissionRepository;

    @Autowired
    private SysUserRoleRepository sysUserRoleRepository;

    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private MerchantRepository merchantRepository;
    @Autowired
    private UserHelper userHelper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private Prop prop;

    /**
     * @api {post} /sso/login_user_token 登录 userToken and domain
     * @apiGroup SSO
     * @apiVersion 1.0.0
     * @apiParam {String} userToken <code>必须参数</code>用户token
     * @apiParam {String} domain <code>必须参数</code>请求域名
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *      "data": {
     *          "id": 2,
     *          "username": "lihua",
     *          "mall": {
     *              "id": 1,
     *              "name": "B123aab",
     *              "hostname": "b1.b.dev.rockcent.com",
     *          },
     *          "merchant": {
     *              "id": 1,
     *              "name": "B1",
     *              "email": "renzijian2@rockcent.com",
     *              "intro": "OE 好嘿劲",
     *              "mallId": 1
     *          }
     *      }
     * }
     */
    @RequestMapping(value = Routes.SSO_LOGIN_USER_TOKEN, method = RequestMethod.POST)
    public JsonResult loginUserToken(String userToken, String domain, HttpServletRequest request, HttpServletResponse response) {
        RestDoing doing = jsonResult -> {
            response.setContentType("application/json");
            if (StringUtils.isBlank(userToken)) {
                jsonResult.custom("10001", "user_token 不能为空", log);
                return;
            }
            String finalDomain = domain;
            if (StringUtils.isBlank(finalDomain)) {
                jsonResult.custom("10002", "domain 不能为空", log);
                return;
            }
            String userInfoId = stringRedisTemplate.boundValueOps(UserInfo.REDIS_USER_TOKEN_PREFIX + userToken).get();
            if (StringUtils.isBlank(userInfoId)) {
                jsonResult.custom("10003", "user_token 无效", log);
            }
            UserInfo userInfo = userInfoRepository.findOne(Long.valueOf(userInfoId));
            if (userInfo == null) {
                jsonResult.custom("10003", "user_token 无效", log);
            }
            UserInfoDto userInfoDto = userHelper.setSession(userInfo,request);
            jsonResult.data=userInfoDto;
        };
        return doing.go(request, log);
    }

}
